// #include "../include/listDataModel.h"
#include "listDataModel.h"

ListDataModel::ListDataModel(){
    initCommandMap();
}
ListDataModel::~ListDataModel(){

}

std::string ListDataModel::executeCommand(const std::string& command, std::istringstream& iss)
{
    if(commandMap.count(command)){
        std::cout << "Executing command: " << command << " ";
        return commandMap[command](iss);
    }
    return "error List " + command + " not found";
}

void ListDataModel::initCommandMap()
{
    commandMap["lpush"] = [this](std::istringstream& iss) -> std::string {
        std::string word;
        while (iss >> word){
            listDataModel.push_front(std::move(word));
        }
        return "OK";
    };
    commandMap["rpush"] = [this](std::istringstream& iss) -> std::string {
        std::string word;
        while (iss >> word){
            listDataModel.push_back(std::move(word));
        }
        return "OK";
    };
    commandMap["lrange"] = [this](std::istringstream& iss) -> std::string {
        std::string word;
        iss >> word;
        int start = std::stoi(word);
        iss >> word;
        int end = std::stoi(word) > listDataModel.size() - 1 ? listDataModel.size() - 1 : std::stoi(word);
        std::string result;
        for (int i = start; i <= end; i++){
            if (i < 0 || i >= listDataModel.size()) continue;
            result += listDataModel[i] + " ";
        }
        return result;
    };
}
